package handler

import (
	"context"
	"log"

	"giikin.oder/db"
	"giikin.oder/kitex_gen/com/giikin/base"
	"giikin.oder/kitex_gen/com/giikin/order"
	"giikin.oder/service"
)

func GetOrderEtcInfo(ctx context.Context, request *order.GetOrderEtcInfoRequest) (resp *order.GetOrderEtcInfoResponse) {
	resp = new(order.GetOrderEtcInfoResponse)
	resp.Base = new(base.BaseResponse)

	if request.GetOrderID() <= 0 {
		resp.Base.Code, resp.Base.Message = -1, "orderID is empty"
		return
	}

	if request.GetType().String() == "<UNSET>" {
		resp.Base.Code, resp.Base.Message = -1, "type is empty"
		return
	}

	etcInfo, err := db.GetOrderEtcInfo(ctx, request.GetOrderID(), request.GetType())
	if err != nil {
		log.Println(err)
		log.Println(resp.Base)
		resp.Base.Code, resp.Base.Message = -1, err.Error()
		return
	}

	resp = &order.GetOrderEtcInfoResponse{
		OrderID: etcInfo.OrderID,
		Type:    etcInfo.Type,
		Value:   etcInfo.Value,
		SubData: etcInfo.SubData,
	}

	return
}

func SaveOrderEtc(ctx context.Context, request *order.SaveOrderEtcRequest) (resp *order.SaveOrderEtcResponse) {
	resp = new(order.SaveOrderEtcResponse)
	resp.Base = new(base.BaseResponse)

	if request.GetOrderID() <= 0 {
		resp.Base.Code, resp.Base.Message = -1, "orderID is empty"
		return
	}

	if request.GetType().String() == "<UNSET>" {
		resp.Base.Code, resp.Base.Message = -1, "type is empty"
		return
	}

	err := service.SaveOrderEtc(ctx, request)
	if err != nil {
		log.Println(err)
		log.Println(resp.Base)
		resp.Base.Code, resp.Base.Message = -1, err.Error()
		return
	}

	return
}
